// pages/film/film.js
var touch_1 = require("./touch");

let plugin = requirePlugin('routePlan');
let key = 'PBHBZ-BM5CI-STHGA-5FUOM-AQGCT-EBBEU'; //使用在腾讯位置服务申请的key
let referer = 'Proton'; //调用插件的app的名称
let endPoint = JSON.stringify({ //终点
  'name': '腾讯',
  'latitude': 23.099994,
  'longitude': 113.324520
});
const app = getApp()
const CDN_PATH = 'https://3gimg.qq.com/lightmap/xcx/demoCenter/images';
const INIT_MARKER = {
  callout: {
    content: '我的影院',
    padding: 10,
    borderRadius: 2,
    display: 'ALWAYS'
  },
  latitude: 23.099994,
  longitude: 113.324520,
  iconPath: './imgs/Marker1_Activated@3x.png',
  width: '34px',
  height: '34px',
  rotate: 0,
  alpha: 1,
  loading: false,
};
Component({
  behaviors: [touch_1],
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isLoad: Boolean,
    currentCity: String,
    addresse: String,
    showingMovie: Array,
    upcomingMovie: Array,
    upcomingMovieBydate: Array,
    maoyanComing: Array,
  },

  /**
   * 组件的初始数据
   */
  data: {
    loadding: false,
    imgs: {
      plus: `${CDN_PATH}/btn_plus@3x.png`,
      minus: `${CDN_PATH}/btn_minus@3x.png`
    },
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabActive: 1,
    reftrigger: false,
    nomore: true,
    markers: [{
      ...INIT_MARKER
    }],
    longitude: '113.324520',
    latitude: '23.099994',
    scale: 15,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    search() {
      wx.navigateTo({
        url: '/pages/search/search',
        success: function (res) {}
      })
    },
    movieinfo(event) {
      var myThis = this;
      var id;
      wx.request({
        url: 'https://wx.maoyan.com/hostproxy/mmdb/search/integrated/keyword/list.json?keyword='+event.currentTarget.dataset.movieinfo.name+'&almtype=1&iscorrected=false&stype=-1&ci=20',
        header: {
          'x-host': 'http://maoyanapi.vip.sankuai.com'
        },
        success(res) {
          id = res.data.data[0].list[0].id
          wx.navigateTo({
            url: '/pages/movieinfo/index?maoyanid='+id,
            success: function (res) {
              // 通过eventChannel向被打开页面传送数据
              res.eventChannel.emit('movieinfo', {
                movieinfo: event.currentTarget.dataset.movieinfo
              })
            }
          })
        }
      })
    },
    maoyaninfo(event) {
      wx.navigateTo({
        url: '/pages/movieinfo/index?maoyanid='+event.currentTarget.dataset.movieinfo.id,
      })
      
    },
    schedule(event) {      
      wx.navigateTo({
        url: '/pages/film/schedule/index?movieinfo='+JSON.stringify(event.currentTarget.dataset.movieinfo),
      })
    },
    refreshloc() {
      this.setData({
        addresse: '正在获取定位...'
      })
      this.getLocation();
    },
    onMap() {
      this.setData({
        loading: true
      })
      var myThis = this;
      wx.navigateTo({
        url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint,
        success: function (res) {
          myThis.setData({
            loading: false
          })
        }
      });
    },
    getLocation: function () {
      var page = this
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          // success  
          var longitude = res.longitude
          var latitude = res.latitude
          wx.request({
            url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=PBHBZ-BM5CI-STHGA-5FUOM-AQGCT-EBBEU&get_poi=1',
            data: {},
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              // success                
              var city = res.data.result.address_component.city;
              var addresse = res.data.result.formatted_addresses.recommend

              city = city.substring(0, (city.length - 1))
              page.setData({
                addresse: addresse
              });
            },
            fail: function () {
              page.setData({
                addresse: "获取定位失败"
              });
            },
          })
        }
      })
    },
    // 控制地图缩放级别
    onIncreaseScale() {
      let scale = this.data.scale;
      if (scale === 20) {
        return;
      }
      scale++;
      this.setData({
        scale: scale
      });
    },
    onDecreaseScale() {
      let scale = this.data.scale;
      if (scale === 3) {
        return;
      }
      scale--;
      this.setData({
        scale: scale
      });
    },
    loadMore() { // 触底加载更多
      console.log('loadMore');
    },
    refresh() {
      var myThis = this
      this.setData({
        loadding: true,
      })
      this.getTab2Info();
      setTimeout(function () {
        myThis.setData({
          loadding: false
        })
      }, 2000)
    },
    tabSelect(e) {
      this.setData({
        TabActive: e.currentTarget.dataset.id,
      })
    },
    onTouchStart: function (event) {
      this.touchStart(event);
    },
    onTouchMove: function (event) {
      this.touchMove(event);
    },
    // watch swipe touch end
    onTouchEnd: function () {
      var _b = this,
        direction = _b.direction,
        deltaX = _b.deltaX,
        offsetX = _b.offsetX;
      var minSwipeDistance = 50;
      if (direction === 'horizontal' && offsetX >= minSwipeDistance) {
        if (deltaX > 0 && this.data.TabActive !== 0) {
          this.setData({
            TabActive: this.data.TabActive - 1
          })

        } else if (deltaX < 0 && this.data.TabActive !== 2) {
          this.setData({
            TabActive: this.data.TabActive + 1
          })
        }
      }
    },
    getTab2Info: function () {
      var myThis = this;
      var now = new Date();
      var nowdate = now.toISOString().slice(0, 10).replace(/-/g,'')
      wx.request({
        url: app.globalData.url + '/wechatapi/showingMovie?date='+nowdate,
        success(res) {
          myThis.setData({
            showingMovie: res.data
          })
        }
      })
      wx.request({
        url: app.globalData.url + '/wechatapi/upcomingfilmdate',
        success(res) {
          myThis.setData({
            upcomingMovieBydate: res.data
          })
        }
      })
      wx.request({
        url: app.globalData.url + '/wechatapi/upcomingMovie',
        success(res) {
          myThis.setData({
            upcomingMovie: res.data
          })
        }
      })
    }
  },
  ready: function () {

  },
  attached: function () {}

})